package com.homer.android.activity.main;
import org.apache.log4j.Logger;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.homer.android.R;
import com.homer.android.sdk.devcie.IEspDevice;
import com.homer.android.sdk.type.user.builder.BEspUser;
import com.homer.android.sdk.user.IEspUser;
import com.homer.push.android.client.Constants;



public class ConfirmActivity extends Activity
{
    
    private static final Logger log = Logger.getLogger(ConfirmActivity.class);
    
    Button btn_confirm;
    
    TextView  txt_message;
    
    protected IEspUser mUser;
    
    String devicekey;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        super.onCreate(savedInstanceState);  
        requestWindowFeature(Window.FEATURE_LEFT_ICON);  
        setTitle("Homer安防套件");// 设置标题  
        setContentView(R.layout.confirm_activity);// 设置布局内容  
        setFinishOnTouchOutside(false);  
        // 设置左边图标  
        getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,  
                android.R.drawable.ic_dialog_alert);  
        mUser = BEspUser.getBuilder().getInstance();
        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        String deviceNumber = bundle.getString(Constants.NOTIFICATION_URI);
        
        String message = bundle.getString(Constants.NOTIFICATION_MESSAGE);
        devicekey = deviceNumber;
        final byte[] bId = hexStringToBytes(deviceNumber);
        btn_confirm = (Button)findViewById(R.id.btn_confirm);
        txt_message = (TextView)findViewById(R.id.txt_message);
        txt_message.setText(message);
        btn_confirm.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					com.homer.android.activity.util.BASE64Encoder encoder = new com.homer.android.activity.util.BASE64Encoder();
					byte[] b = new byte[11];
					b[0] = 0x5A;
					b[1] = bId[0];
					b[2] = bId[1];
					b[3] = bId[2];
					b[4] = bId[3];
					b[5] = 0x00;
					b[6] = 0x00;
					b[7] = (byte)0x04;
					b[8] = 0x01;
					b[9] = (byte)0x00;
					b[10] = (byte) 0xA5;
					String param = encoder.encode(b);
					
					executePost(param);
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				
				Intent intent = new Intent(ConfirmActivity.this,EspUIActivity.class);
				startActivity(intent);
				ConfirmActivity.this.finish();

			}
		});
    }
    protected static final int COMMAND_POST = 1;
    protected void executePost(final String base64Param) {
		new DeviceTask(this).execute(base64Param);
	}

	private class DeviceTask extends AsyncTask<String, Void, Object>
			implements OnDismissListener {
		private Activity mActivity;

		private ProgressDialog mDialog;

		private int mCommand;

		public DeviceTask(Activity activity) {
			mActivity = activity;
		}

		@Override
		protected void onPreExecute() {
			//executePrepare();

			//showDialog();
		}

		@Override
		protected Object doInBackground(String... params) {
			try {
				if (params.length > 0) {
					// execute Post status
					//log.debug(mIEspDevice.getName() + " Post status");
					String status = params[0];
					mCommand = COMMAND_POST;
					
					IEspDevice mIEspDevice = mUser
							.getUserDevice(devicekey);
						return mUser.doActionPostDeviceStatus(mIEspDevice, status);
					
				} 
				else{
					return null;
				}
			} catch (Exception e) {
				return null;
			}
			
		}

		@Override
		protected void onPostExecute(Object result) {
			log.debug("DeviceTask result = " + result);
			//releaseDialog();

			executeFinish(mCommand, result);
		}

		private void showDialog() {
			mDialog = new ProgressDialog(mActivity);
			mDialog.setMessage(getString(R.string.esp_device_task_dialog_message));
			mDialog.setCanceledOnTouchOutside(false);
			mDialog.setOnDismissListener(this);
			mDialog.show();
		}

		private void releaseDialog() {
			if (mDialog != null) {
				mDialog.dismiss();
				mDialog = null;
			}
		}

		@Override
		public void onDismiss(DialogInterface dialog) {
			cancel(true);
			mDialog = null;
		}
	}
	protected void executeFinish(int command, Object result) {
		
	}
    
    /**
	 *    * Convert hex string to byte[]    * @param hexString the hex string  
	 *  * @return byte[]    
	 */
	public byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	private byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}
    
}
